//
//  AppDelegate.m
//  SHHCPhone
//
//  Created by sheng long on 14-6-15.
//  Copyright (c) 2014年 sheng long. All rights reserved.
//

#import "AppDelegate.h"
#import "LoginViewController.h"

#import "MobileData.h"

#import "MMDrawerController.h"
#import "HomeSideViewController.h"
#import "HomeMainViewController.h"

#import "MobClick.h"

@implementation AppDelegate

+ (AppDelegate *)getAppDelegate
{
    return (AppDelegate *) [[UIApplication sharedApplication] delegate];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    //数据库
    [MobileData openSqlite];
    
    //友盟
    [MobClick startWithAppkey:UMENG_APPKEY reportPolicy:BATCH channelId:nil];
    
    if (CURRENT_VERSION_SEVEN) {
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
    }else{
        [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleBlackOpaque];
    }
    
    UINavigationController *nav = nil;
//    NSString *user = [APP_USERDEFAULTS valueForKey:APP_LOGIN_SUCESS_SVAE_MSG];
//    if (user) {
//        //用户已登录
//        self.userMsg = [[UserMsg alloc] init:[user objectFromJSONString]];
//        nav = [[UINavigationController alloc] initWithRootViewController:[self getHomeVC]];
//        [self performSelector:@selector(addLogin:) withObject:nav afterDelay:1];
//    }else{
        nav = [[UINavigationController alloc] initWithRootViewController:[[LoginViewController alloc]init]];
//    }
    [nav.navigationBar setHidden:YES];
    
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    [self.window setRootViewController:nav];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
}

//添加登录页面
-(void)addLogin:(UINavigationController*)nav
{
    NSMutableArray *array = [NSMutableArray arrayWithArray:nav.viewControllers];
    [array insertObject:[[LoginViewController alloc] init] atIndex:0];
    nav.viewControllers = array;
}

//获取首页
-(UIViewController*)getHomeVC
{
    //登录信息置空
    [[NSNotificationCenter defaultCenter] postNotificationName:LOGINVIEW_MSG_NULL object:nil];
    
    NSString *user = [APP_USERDEFAULTS valueForKey:APP_LOGIN_SUCESS_SVAE_MSG];
    if (!user && self.userDic) {
        //保存用户信息
        [APP_USERDEFAULTS setValue:[self.userDic JSONString] forKey:APP_LOGIN_SUCESS_SVAE_MSG];
    }
    
    HomeSideViewController * leftSideDrawerViewController = [[HomeSideViewController alloc] init];
    
    HomeMainViewController * centerViewController = [[HomeMainViewController alloc] init];
    
    UINavigationController * navigationController = [[UINavigationController alloc] initWithRootViewController:centerViewController];
    [navigationController.navigationBar setHidden:YES];
    
    MMDrawerController * drawerController = [[MMDrawerController alloc]
                                             initWithCenterViewController:navigationController
                                             leftDrawerViewController:leftSideDrawerViewController
                                             rightDrawerViewController:nil];
    [drawerController setMaximumLeftDrawerWidth:HOME_SIDE_WIDTH];
    [drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
    [drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
    
    return drawerController;
}

@end
